<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Large Model Interaction</title>
    <!-- Bootstrap CSS -->
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <!-- jQuery -->
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <!-- Font Awesome -->
    <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css" rel="stylesheet">
    <style>
        body {
            font-family: 'Inter', sans-serif;
            background-color: #f8f9fa;
        }

        .chat-container {
            height: 600px;
            overflow-y: auto;
            padding: 15px;
            border: 1px solid #dee2e6;
            border-radius: 0.25rem;
            background-color: #fff;
        }

        .chat-message {
            padding: 10px 15px;
            margin-bottom: 10px;
            border-radius: 8px;
            max-width: 80%;
        }

        .user-message {
            background-color: #e2f1ff;
            margin-left: auto;
            text-align: right;
        }

        .model-message {
            background-color: #f0f0f0;
            margin-right: auto;
        }

        .input-group {
            margin-top: 15px;
        }
    </style>
</head>

<body>
    <div class="container py-5">
        <div class="row justify-content-center">
            <div class="col-md-8">
                <div class="card shadow">
                    <div class="card-header bg-primary text-white">
                        <h3 class="text-center mb-0">Large Model Interaction</h3>
                    </div>
                    <div class="card-body">
                        <div id="chat-container" class="chat-container mb-3">
                            <!-- Chat messages will be added here -->
                        </div>
                        <div class="input-group">
                            <input type="text" id="input-question" class="form-control" value="你是谁"
                                placeholder="Ask your question...">
                            <button id="send-question" class="btn btn-primary">
                                <i class="fas fa-paper-plane"></i>
                            </button>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <!-- Bootstrap JS Bundle with Popper -->
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
    <!-- Marked.js for Markdown parsing -->
    <script src="https://cdn.jsdelivr.net/npm/marked/marked.min.js"></script>
    <script>
        $(document).ready(function () {
            // Send message when button is clicked
            $('#send-question').on('click', function () {
                sendMessage();
            });

            // Send message when Enter key is pressed
            $('#input-question').on('keypress', function (e) {
                if (e.which === 13) { // Enter key
                    sendMessage();
                }
            });

            function sendMessage() {
                const question = $('#input-question').val().trim();
                if (question === '') return;

                // Display user's question
                $('#chat-container').append(
                    $('<div class="chat-message user-message"></div>').text(question)
                );

                // Add loading spinner
                $('#chat-container').append(
                    $('<div class="text-center my-2"><div class="spinner-border text-primary" role="status"><span class="visually-hidden">Loading...</span></div></div>')
                );

                // Scroll to bottom of chat container
                scrollToBottom();

                // Send request to server
                $.ajax({
                    url: 'http://localhost:8080/chat',
                    type: 'POST',
                    contentType: 'application/json',
                    data: JSON.stringify({ message: question }),
                    success: function (data) {
                        // Remove loading spinner
                        $('#chat-container div.text-center').last().remove();

                        // Display model's response
                        $('#chat-container').append(
                            $('<div class="chat-message model-message"></div>').html(marked.parse(data.content))
                        );
                        scrollToBottom();
                    },
                    error: function (error) {
                        // Remove loading spinner
                        $('#chat-container div.text-center').last().remove();

                        console.error('Request failed:', error);
                        $('#chat-container').append(
                            $('<div class="chat-message model-message"></div>').text('An error occurred while getting the response.')
                        );
                        scrollToBottom();
                    }
                });

                // Clear the input field
                $('#input-question').val('');
            }

            function scrollToBottom() {
                const chatContainer = document.getElementById('chat-container');
                chatContainer.scrollTop = chatContainer.scrollHeight;
            }
        });
    </script>
</body>

</html>